<?php
//echo "Hello";
$host = '10.0.6.54';
$database = 'postgis';
$user = 'postgres';
$pswd = 'gagra321';

$dbcon = pg_connect("host=$host dbname=$database user=$user password=$pswd");
if (!$dbcon)
{
die ("Could not open connetion to database server");
}

$code = $_REQUEST['name'];
//$code = '1225055100011729001';
$koatuu = substr($code,0,10);
$zona = substr($code,10,2);
$kvartal = substr($code,12,3);
$parcel = substr($code,15,4);
//echo $code;
//echo "<br>".$koatuu.":".$zona.":".$kvartal.":".$parcel;

$str = "SELECT * FROM dilanka WHERE koatuu=".$koatuu." and zona=".$zona." and kvartal=".$kvartal." and parcel=".$parcel;
//$theGeom = "SELECT the_geom_900913 FROM dilanka WHERE koatuu=".$koatuu." and zona=".$zona." and kvartal=".$kvartal." and parcel=".$parcel;
//echo "<br>".$str."|";

$result = pg_query($dbcon, "select ST_XMin(ST_Union(the_geom_900913)) as st_xmin,ST_YMin(ST_Union(the_geom_900913)) as st_ymin,ST_XMax(ST_Union(the_geom_900913)) as st_xmax,ST_YMax(ST_Union(the_geom_900913)) as st_ymax from ($str) AS foo");
//$result = pg_query($dbcon, $str);
$row = pg_fetch_array($result);

if (!empty($row['st_xmin'])&&!empty($row['st_ymin'])&&!empty($row['st_xmax'])&&!empty($row['st_ymax'])){
	echo $row['st_xmin'].",".$row['st_ymin'].",".$row['st_xmax'].",".$row['st_ymax'];
}
else {
echo "82,54,84,56";
}
?>
